package com.gdpu.project.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gdpu.project.dao.ProMonitorDao;
import com.gdpu.project.vo.ProMonitor;

public class ProMonitorDaoImpl extends HibernateDaoSupport implements ProMonitorDao {
	private static final Log log = LogFactory.getLog(ProMonitorDaoImpl.class);
	// property constants
	public static final String PROJECT_ID = "projectId";
	public static final String TEACHER_ID = "teacherId";
	public static final String FILE_NAME = "fileName";
	public static final String FILE_PATH = "filePath";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#save(com.gdpu.project.vo.ProMonitor)
	 */
	public void save(ProMonitor transientInstance) {
		log.debug("saving ProMonitor instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#delete(com.gdpu.project.vo.ProMonitor)
	 */
	public void delete(ProMonitor persistentInstance) {
		log.debug("deleting ProMonitor instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findById(java.lang.Integer)
	 */
	public ProMonitor findById(java.lang.Integer id) {
		log.debug("getting ProMonitor instance with id: " + id);
		try {
			ProMonitor instance = (ProMonitor) getHibernateTemplate().get(
					"com.gdpu.project.vo.ProMonitor", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findByExample(com.gdpu.project.vo.ProMonitor)
	 */
	public List<ProMonitor> findByExample(ProMonitor instance) {
		log.debug("finding ProMonitor instance by example");
		try {
			List<ProMonitor> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List<ProMonitor> findByProperty(String propertyName, Object value) {
		log.debug("finding ProMonitor instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ProMonitor as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findByProjectId(java.lang.Object)
	 */
	public List<ProMonitor> findByProjectId(Object projectId) {
		return findByProperty(PROJECT_ID, projectId);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findByTeacherId(java.lang.Object)
	 */
	public List<ProMonitor> findByTeacherId(Object teacherId) {
		return findByProperty(TEACHER_ID, teacherId);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findByFileName(java.lang.Object)
	 */
	public List<ProMonitor> findByFileName(Object fileName) {
		return findByProperty(FILE_NAME, fileName);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findByFilePath(java.lang.Object)
	 */
	public List<ProMonitor> findByFilePath(Object filePath) {
		return findByProperty(FILE_PATH, filePath);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#findAll()
	 */
	public List<ProMonitor> findAll() {
		log.debug("finding all ProMonitor instances");
		try {
			String queryString = "from ProMonitor";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#merge(com.gdpu.project.vo.ProMonitor)
	 */
	public ProMonitor merge(ProMonitor detachedInstance) {
		log.debug("merging ProMonitor instance");
		try {
			ProMonitor result = (ProMonitor) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#attachDirty(com.gdpu.project.vo.ProMonitor)
	 */
	public void attachDirty(ProMonitor instance) {
		log.debug("attaching dirty ProMonitor instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProMonitorDao#attachClean(com.gdpu.project.vo.ProMonitor)
	 */
	public void attachClean(ProMonitor instance) {
		log.debug("attaching clean ProMonitor instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ProMonitorDao getFromApplicationContext(ApplicationContext ctx) {
		return (ProMonitorDao) ctx.getBean("ProMonitorDAO");
	}
}